'use strict';

angular.module('app')
.controller('performanceController', [ '$scope', '$rootScope', '$state', '$timeout', 'toaster',
                                function($scope, $rootScope, $state, $timeout, toaster) {

		$scope.title = '设备运行情况';
        $scope.loading = false;
        $scope.param = {};
		var Nowdate = new Date();
        var years = Number(Nowdate.getFullYear());
        var Month = Number(Nowdate.getMonth())+1;
        if(Month<10){
            Month = '0'+Month
        }
        var nowMonth = years+''+Month;//获取当前年月
        $scope.YYYMM = nowMonth;//赋值到下拉框
        $scope._MM = Month;
        getperformance({'month':nowMonth});//向后台申请数据(默认加载当前月数据);
        getDropStorage({'month':nowMonth});

         //获取近六个月的月份
         var getMonths = function() {  
                var d = new Date();  
                var MonthArray = [];  
                for(var i = 0; i < 6; i++) {  
                    d.setMonth(d.getMonth() - 1);  
                    var m = d.getMonth() + 1;  
                    m = m < 10 ? "0" + m : m;  
                    //在这里可以自定义输出的日期格式  
//                  result.push(d.getFullYear() + "-" + m);  
                    MonthArray.push(d.getFullYear() + "" + m );  
                }  
                return MonthArray;  
            }  
        
        $scope.mm = getMonths();//查询的下拉月份
        //查询请求数据
        $scope.getReportDtae = function () {
            var obj = $('#searchBtn').find("option:selected").data('code');
            getperformance({'month':obj});//请求数据
            getDropStorage({'month':obj});
        }
        //门禁稳定状态
        function getperformance(obj) {
            $scope.loading = true;
            var myChart = echarts.init(document.getElementById('stability'));
            // 异步加载数据
            $.ajax({
        		type: 'POST',
        		dataType: 'json',
        		contentType:'application/json;charset=UTF-8',
        		url : '/dac/report/devicerept/isstable',
        		data: angular.toJson(obj)
    		}).then(function (result) {
                $scope.loading = false;
                console.log(result)
                    function getperform(){
                        var series = [];
                        for(var i = 0; i < result.data.length; i++){
                            var item = {
                                value: result.data[i].value,
                                name:result.data[i].name
                            }
                            series.push(item );
                        };
                        return series;
                    }
                    function labelData(){
                        var labelArray = [];
                        for(var i=0;i<result.data.length;i++){
                            labelArray.push(result.data[i].name)
                        }
                        return labelArray
                    }

                    myChart.setOption({
                        title: {
                            text: '门禁稳定情况',
                            left:'center',
                            fontWeight:'normal'
                        },
                        tooltip : {
                            trigger: 'item',
                            formatter: "{a} <br/>{b} : {c} ({d}%)"
                        },
                        legend: {
                            orient: 'vertical',
                            x: 'left',
                            data:labelData()
                        },
                        color:['#fc7009','#27c24c'],
                        series:[{
                            type: 'pie',
                            radius : '55%',
                            center: ['50%','60%'],
                            min:'1',
                            data:getperform(),
                            itemStyle: {
                                emphasis: {
                                    shadowBlur: 10,
                                    shadowOffsetX: 0,
                                    shadowColor: 'rgba(0, 0, 0, 0.5)'
                                },
                                normal:{
                                    label:{
                                        show: true,
                                        formatter: '{b} : {c} ({d}%)'
                                    },
                                    labelLine :{show:true}
                                }
                            }
                        }]

                    });
            });
        }

        //掉线设备厂家分布
        function getDropStorage(obj) {
            var myChart = echarts.init(document.getElementById('DropStorage'));
            // 异步加载数据
            $.ajax({
                type: 'POST',
                dataType: 'json',
                contentType:'application/json;charset=UTF-8',
                url : '/dac/report/devicerept/isstableproduct',
                data: angular.toJson(obj)
            }).then(function (result) {
                console.log(result)
                $scope.loadding = false;

                    function getDrop(){
                        $scope.Dropseries = [];
                        $scope.NoData = [{value:'0',name:'暂无数据'}];
                        for(var i = 0; i < result.data.length; i++){
                            var item = {
                                value: result.data[i].value,
                                name:result.data[i].name
                            }
                            $scope.Dropseries.push(item );
                        };
                        if($scope.Dropseries.length>0){
                            return $scope.Dropseries;
                        }else{
                            return $scope.NoData;
                        }
                    }

                    //返回标签数据
                    function labelData (){
                        $scope.labelArray = [];
                        $scope.NoData =['暂无数据'];
                        for(var i=0;i<result.data.length;i++){
                            $scope.labelArray.push(result.data[i].name);
                        }

                        if($scope.labelArray.length>0){
                            return $scope.labelArray;
                        }else{
                            return $scope.NoData
                        }
                    }

                    myChart.setOption({

                    title: {
                        text: '设备掉线厂家分布',
                        left:'center',
                        fontWeight:'normal'
                    },
                    tooltip : {
                        trigger: 'item',
                        formatter: "{a} <br/>{b} : {c} ({d}%)"
                    },
                    legend: {
                        orient: 'vertical',
                        x: 'left',
                        data:labelData()
                    },
                    color:['#6490f2','#ec0e6e'],
                    series:[{
                        type: 'pie',
                        radius : '55%',
                        center: ['50%','60%'],
                        min:'1',
                        data:getDrop(),
                        itemStyle: {
                            emphasis: {
                                shadowBlur: 10,
                                shadowOffsetX: 0,
                                shadowColor: 'rgba(0, 0, 0, 0.5)'
                            },
                            normal:{
                                label:{
                                    show: true,
                                    formatter: '{b} : {c} ({d}%)'
                                },
                                labelLine :{show:true}
                            }
                        }
                    }]

                }); 
                
            })
        }


        //设备列表
        $scope.search = function(){
            if($scope.pageFlag != 1){//未点击翻页
                $scope.param.pageNum=1;//将页码设置为1，即跳转回第一页
            }else{//点击翻页
                $scope.pageFlag = 2;//将开关值设置为2
            }

            $.ajax({
                type: 'POST',
                dataType: 'json',
                contentType:'application/json;charset=UTF-8',
                url : '/dac/report/devicerept/list',
                data: angular.toJson($scope.param)
            }).then(function (result) {
                console.log(result)
                if(result.data.length>0){
                    $scope.pageInfo = result
                }else{
                    console.log(result.msg);
                }
                
            })
        }

        $scope.search();
		// 翻页
        $scope.pagination = function (page) {
            $scope.param.pageNum=page;
            $scope.pageFlag = 1;//将开关值设置为1
            $scope.search();
        };
} ]);